package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseMarketService;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.security.Security;
import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
//@RequestMapping("courseBase")
public class CourseBaseController implements CourseBaseApi {


    @Autowired
    private CourseBaseService  courseBaseService;

    @Autowired
    private CoursePubService coursePubService;


    @Override
    @PostMapping("/course/list")
    public PageVO queryCourtentList(PageRequestParams params,
                                    @RequestBody QueryCourseModel model) {

        Long companyId = SecurityUtil.getCompanyId();

        PageVO pageVo = courseBaseService.queryCourseBaseList(params,model,companyId);

        return pageVo;
    }

    @PostMapping("/course")
    public CourseBaseDTO courseBasseDto(@RequestBody CourseBaseVO courseBaseVO) {


        //将VO转换为DTO
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.voentit2dto(courseBaseVO);

        //获取公司id
        Long companyId = SecurityUtil.getCompanyId();

        //设置公司和登录信息
        dto.setCompanyId(companyId);

        CourseBaseDTO relius = courseBaseService.courseBasseDto(dto);
        return relius;
    }


    @Override
    @GetMapping("/course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {


        Long companyId = SecurityUtil.getCompanyId();

        CourseBaseDTO dto = courseBaseService.getCourseBase(courseBaseId,companyId);

        return dto;
    }


    @Override
    @PutMapping("/course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {

        //将数据转换为dto实体类
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.voentit2dto(courseBaseVO);

        //获取公司id和用户信息
        Long companyId = SecurityUtil.getCompanyId();
        dto.setCompanyId(companyId);

        CourseBaseDTO courseBaseDTO = courseBaseService.modifyCourseBase(dto);


        return courseBaseDTO;
    }

    @Override
    @DeleteMapping("/course/{courseBaseId}")
    public void removeCoursebase(@PathVariable Long courseBaseId) {

        //获取公司id
        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.removeCoursebase(courseBaseId,companyId);

    }

    @GetMapping("/course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {

        //获取公司id
        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.commitCourseBase(courseBaseId,companyId);

    }


    @GetMapping("/course/preview/{courseBaseId}/{companyId}")
    public Object previewCouse(@PathVariable("courseBaseId") Long courseId, @PathVariable Long companyId) {

        Map<String, Object> dateMap = coursePubService.previewCourse(courseId, companyId);

        //1.使用试图和数据模型对象

        ModelAndView modelAndView = new ModelAndView("learing_article",dateMap);


        return modelAndView;
    }

}
